<?php
// $Id$

/**
 * Controller_Admin_Members 控制器
 */
class Controller_Admin_Members extends Controller_Abstract
{

    function actionIndex()
    {
        //这里是搜索字段和操作符，like就是模糊搜索了，=就是严格等于，很好理解
        $wheres = array('username'=>'like', 'depart_id'=>'=');
        $this->_getJQGridData(User::find(), $wheres, 'roles, fields, department');

        //        $condi = new QDB_Cond(
        //            'username like ?', '%'.$this->_context->keyword.'%'
        //        );
        //        $page = $this->_context->page;
        //        $pagesize = Q::ini('appini/background/member_list_pagesize');
        //        $members = User::find($condi)->limitPage($page, $pagesize)->order('created DESC')->query();
        //        $records = User::find($condi)->all()->getCount('user_id');
        //        //        echo $records;
        //        $this->_view['records'] = $records;
        //        $this->_view['pagesize'] = $pagesize;
        //        $this->_view['members'] = $members;
        //        $this->_view['keyword'] = $this->_context->keyword;
    }

    function actionList() {
        $condi[] = new QDB_Expr(
            "username LIKE '%" .$this->_context->username. "%' "
        );

        $this->_find(User::find(), $condi);
    }

    function actionRegister() {
        $this->_view['group_droplist'] = Role::droplist(array('name'=>'roles[]'));
    }

    function actionSavereg() {
        User::savereg();
        return $this->_redirectMessage('网站提示','用户注册成功', url(''));
    }

    function actionTrans() {
        $user = Helper_RBAC::get('user');
        $roles = Helper_RBAC::roles('user');
        $idset = FUNC::getIdset($this->_context->idset);
        foreach($idset as $id) {
            $record = User::find('user_id = ?', $id)->one()->query();
            if(in_array('ADMIN', $roles) && $record->id()) {
                $record->depart_id = $this->_context->depart_id;
                $record->save();
                //                Role::meta()->destroyWhere('role_id = ?', $id);
            }
        }
    }

    function actionTransGroup() {
        $user = Helper_RBAC::get('user');
        $roles = Helper_RBAC::roles('user');
        $idset = FUNC::getIdset($this->_context->idset);
//        dump($idset);
        foreach($idset as $id) {
            $record = User::find('user_id = ?', $id)->one()->query();
//            echo 'find|';
            if(in_array('ADMIN', $roles) && $record->id()) {
//echo 'in';

                $rolesets = FUNC::getIdset(FUNC::getV('roles'));
                //            dump($roles);
                //            $role = Role::find('role_id = ?', 0)->one()->query();
                //            $record -> roles = $role;
                //            $record -> save();
                if($record->roles->id()){
                    foreach($record -> roles as $key=>$r) {
                        unset($record->roles[$key]);
                    }
                }

//                dump($record -> roles);
                foreach($rolesets as $role_id) {
                    $role = Role::find('role_id = ?', $role_id)->one()->query();
                    if($role->id()) {
                        $record -> roles[] = $role;
                    }
                }
//                dump($record -> roles);

                //                $record->depart_id = $this->_context->depart_id;
                $record->save();
                //                Role::meta()->destroyWhere('role_id = ?', $id);
            }
        }
    }

    function actionSave() {
        $condi[] = new QDB_Cond(
            'user_id = ?', $this->_context->user_id
        );
        $member = User::find($condi)->one()->query();
        $member->saveedit();
        $referer = $this->_context->referer() ? $this->_context->referer() : url('');
        return $this->_redirectMessage('网站提示','用户资料修改成功', $referer);
    }

    function actionRemove() {
        $user = Helper_RBAC::get('user');
        $roles = Helper_RBAC::roles('user');
        $idset = FUNC::getIdset($this->_context->idset);
        foreach($idset as $id) {
            $record = User::find('user_id = ?', $id)->one()->query();
            if(in_array('ADMIN', $roles)) {
                User::meta()->destroyWhere('user_id = ?', $id);
            }
        }
    }

    /**
     * 查看详细资料
     */
    function actionInfo() {
        $condi[] = new QDB_Cond(
            'user_id = ?', $this->_context->user_id
        );
        $member = User::find($condi)->one()->query();
        //        Qdebug::dump($member);
        $this->_view['member'] = $member;
    }

    /**
     * 编辑用户
     */
    function actionEdit() {
        $condi[] = new QDB_Cond(
            'user_id = ?', $this->_context->user_id
        );
        $member = User::find($condi)->one()->query('roles');
        //        Qdebug::dump($member);
        //                Qdebug::dump($member);
        //                dump($member->roles);
        $selected = FUNC::setArrayByField(array('role_id'), $member->roles->toArray());
        //                dump($selected);
        $this->_view['group_droplist'] = Role::droplist(array('name'=>'roles[]'),$selected);
        $this->_view['member'] = $member;
    }

}


